<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2019/1/10
 * Time: 20:35
 */
class Ws{
    const HOST = "0.0.0.0";
    const PORT = 8812;
    private $ws_serv=null;
    private $set=[];
    private $static_path='/apps/wwwroot/swoole/data'; //默认静态资源路径
    public function __construct()
    {
        $this->ws_serv = new swoole_websocket_server(self::HOST, self::PORT);
        $this->websocketSet($this->static_path);
        $this->ws_serv->on('open',[$this,'onOpen']);
        $this->ws_serv->on('message',[$this,'onMessage']);
        $this->ws_serv->on('task',[$this,'onTask']);
        $this->ws_serv->on('finish',[$this,'onFinish']);
        $this->ws_serv->on('close',[$this,'onClose']);
        $this->ws_serv->start();
    }
    private function websocketSet($path,$static=true){
        $this->ws_serv->set([
            'enable_static_handler' => $static,
            'document_root' => $path ,
            'task_worker_num' => 8 , //task 任务 必须设置
        ]);
    }
    /**
     * websocket 客户端连接的时候 回调
     * @param swoole_websocket_server $server
     * @param $request
     */
    public function onOpen(swoole_websocket_server $server, swoole_http_request $request)
    {
        if($request->fd == 3){
            swoole_timer_tick(2000,function ($timer_id){
                echo "timer id is {$timer_id}".PHP_EOL;
            });
        }
        echo "Client {$request->fd} connect".PHP_EOL;
    }

    /**
     * 接收到客户端消息的回调
     * @param swoole_websocket_server $server
     * @param swoole_websocket_frame $frame
     */
    public function onMessage(swoole_websocket_server $server, swoole_websocket_frame $frame)
    {
        echo "Client {$frame->fd} send message :{$frame->data} ; dataType is {$frame->opcode}".PHP_EOL;
        // 如果此时业务逻辑复杂 耗时的话 我使用异步任务
        $data=[
            'task' => 1,
            'fd' =>$frame->fd
        ];
//        $server->task($data);
        if ($frame->fd == 3 ) {
            swoole_timer_after(8000,function ()use ($server,$frame){
                $server->push($frame->fd,"web_socket timer push message ".date('Y-m-d H:i:s'));
            });
        }

//        $server->push($frame->fd,"web_socket push message ".date('Y-m-d H:i:s'));
    }

    /**
     * 异步任务投递
     * @param swoole_websocket_server $server
     * @param $taskid
     * @param $workid
     * @param $data
     * @return string
     */
    public function onTask(swoole_websocket_server $server, $taskid, $workid, $data)
    {
        print_r($data);
        echo PHP_EOL;
        sleep(10);
        return "taskid {$taskid} return , notice woker id {$workid} finsh";
    }

    /**
     * 异步任务的woker进程 回调onfinish
     * @param swoole_websocket_server $server
     * @param $taskid
     * @param $data
     */
    public function onFinish(swoole_websocket_server $server, $taskid, $data)
    {
        echo "task {$taskid} is finish ,result :".$data .PHP_EOL;
    }

    /**
     * 客户端关闭时触发的回调
     * @param swoole_websocket_server $server
     * @param swoole_websocket_frame $frame
     */
    public function onClose(swoole_websocket_server $server, $fd)
    {
        echo "Client {$fd} closed".PHP_EOL;
    }

}
$ws_s=new Ws();